// 将后端返回的列表改成树结构，必须有id、pid字段，根节点的pid必须为0，
export function transList2Tree(list) {
  const roots = list.filter((item) => item.pid === 0)
  transNodeList2Tree(roots, list)
  return roots
}

const transNodeList2Tree = (nodeList, list) => {
  nodeList.forEach((item) => {
    list.forEach((e) => {
      if (item.id === e.pid) {
        if (item.children === undefined) item.children = []
        item.children.push(e)
      }
    })
    if (item.children !== undefined && item.children.length > 0) {
      transNodeList2Tree(item.children, list)
    }
  })
}
